//var fs = require('fs');
//var path = require('path');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var dateFormat = require('dateformat');
var runSequence = require('run-sequence');
var pkg = require('./package.json');

var versionForNocache = 'r=1001';
var dirs = pkg['h5bp-configs'].directories;
dirs.dist = dirs.dist + '/' + dateFormat(new Date(), 'yyyymmdd');
var settings = {};
settings.zipcss = true;


gulp.task('copy:vendor', function () {
    gulp.src(['bower_components/angular/angular.min.js'])
        .pipe(plugins.rename('angular.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-animate/angular-animate.min.js'])
        .pipe(plugins.rename('angular-animate.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-ui-router/release/angular-ui-router.min.js'])
        .pipe(plugins.rename('angular-ui-router.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));

    gulp.src(['bower_components/angular-aria/angular-aria.min.js'])
        .pipe(plugins.rename('angular-aria.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-material/angular-material.min.js'])
        .pipe(plugins.rename('angular-material.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));

    gulp.src(['bower_components/angular-messages/angular-messages.min.js'])
        .pipe(plugins.rename('angular-messages.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));

    gulp.src(['bower_components/angular-cookies/angular-cookies.min.js'])
        .pipe(plugins.rename('angular-cookies.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));

    gulp.src(['bower_components/jQuery/dist/jquery.min.js'])
        .pipe(plugins.rename('jquery.js'))
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));


    gulp.src(['bower_components/angular-material/angular-material.min.css'])
        .pipe(plugins.rename('angular-material.css'))
        .pipe(gulp.dest(dirs.dist + '/css/vendor'));

    gulp.src(['bower_components/font-awesome/css/font-awesome.min.css'])
        .pipe(plugins.rename('font-awesome.css'))
        .pipe(gulp.dest(dirs.dist + '/css/vendor'));

    gulp.src(['bower_components/font-awesome/fonts/*'])
        .pipe(gulp.dest(dirs.dist + '/css/fonts/'));

    // map
    gulp.src(['bower_components/angular/angular.min.js.map'])
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-animate/angular-animate.min.js.map'])
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-aria/angular-aria.min.js.map'])
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-messages/angular-messages.min.js.map'])
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
    gulp.src(['bower_components/angular-cookies/angular-cookies.min.js.map'])
        .pipe(gulp.dest(dirs.dist + '/js/vendor'));
});

gulp.task('copy:js', function () {
    gulp.src([dirs.src + "/Share/app.js",
            dirs.src + "/**/*Service.js",
            dirs.src + "/**/*Controller.js"])
        .pipe(plugins.concat("main.js"))
        .pipe(gulp.dest(dirs.dist));
});


gulp.task('copy:index', function () {
    return gulp.src(['src/Share/index.html']).pipe(plugins.replace(/{{version}}/g, versionForNocache)).pipe(gulp.dest(dirs.dist));
});

//gulp.task('copy:template', function () {
//    gulp.src(['/**/_*.html']).pipe(gulp.dest(dirs.dist+'/Template'));
//    return gulp.src(['src/Share/index.html']).pipe(gulp.dest(dirs.dist));
//});

gulp.task('clean', function (done) {
    require('del')([
        dirs.archive,
        dirs.dist
    ]).then(function () {
        done();
    });
});

gulp.task('copy:css', function () {
    var banner = '/*! 悦筹 v' + pkg.version +
        ' | ' + pkg.license.type + ' License' +
        ' | ' + pkg.homepage + ' */\n\n';

    var result = gulp.src(dirs.src + '/Share/main.less').pipe(plugins.less({
        paths: [dirs.src]
    })).pipe(plugins.header(banner));

    if (settings.zipcss) {
        result = result.pipe(plugins.minifyCss());
    }
    return result.pipe(plugins.autoprefixer({
        browsers: ['ie >= 8', '> 1%'],
        cascade: false
    })).pipe(gulp.dest(dirs.dist + '/css'));
});

gulp.task('copy:misc', function () {
    return gulp.src([
        dirs.src + '/**/*',
        '!' + dirs.src + 'js',
        '!' + dirs.src + '/**/*.ts',
        '!' + dirs.src + '/Share/App.js',
        '!' + dirs.src + '/Share/index.html',
        '!' + dirs.src + '/**/*.less',
    ], {dot: true}).pipe(gulp.dest(dirs.dist));
});

gulp.task('copy', [
    'copy:misc',
    'copy:js',
    'copy:index',
    'copy:css',
    'copy:vendor'
]);

gulp.task('lint:js', function () {
    return gulp.src([
        'gulpfile.js'
    ]).pipe(plugins.jscs())
        .pipe(plugins.jshint())
        .pipe(plugins.jshint.reporter('jshint-stylish'))
        .pipe(plugins.jshint.reporter('fail'));
});

gulp.task('build', function (done) {
    runSequence(
        ['clean', 'lint:js'],
        'copy',
        done);
});

gulp.task('default', ['build']);
